package top.someapp.dxf2dsvg.dxf;

import javax.annotation.Nonnull;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * HEADER Section
 *
 * @author zwz
 * Created on 2020-04-17
 * @see <a href="http://help.autodesk.com/view/OARX/2018/CHS/?guid=GUID-A85E8E67-27CD-4C59-BE61-4DC9FADBE74A">HEADER 段组码 (DXF)</a>
 */
public class SectionHeader implements Section {
    private Map<String, String> variables = new LinkedHashMap<>();
    private Item previous;

    @Override
    public SectionName name() {
        return SectionName.HEADER;
    }

    @Override
    public List<Item> items() {
        return null;
    }

    @Override
    public void onItemFound(@Nonnull Item item) {
        if (item.value.startsWith("$")) {
            if (previous == null) {
                variables.put(item.value, "");
            }
            previous = item;
        } else {
            variables.put(previous.value, item.value.trim());
        }
    }

    public String variableValue(String name, String defaultValue) {
        String value = variables.get(name);
        return value == null ? defaultValue : value;
    }

    @Override
    public String toString() {
        return "SectionHeader{" +
                "$ACADVER=" + variables.get("$ACADVER") +
                ", $DWGCODEPAGE=" + variables.get("$DWGCODEPAGE") +
                '}';
    }
}
